package cn.zlg.www.view;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.NumberPicker;

import com.sany.canwifi.R;
public class IpAlertDialog extends AlertDialog.Builder {

	public static final int IP_PART1 = 0;
	public static final int IP_PART2 = 1;
	public static final int IP_PART3 = 3;
	public static final int IP_PART4 = 4;
	private NumberPicker mIpPart1, mIpPart2, mIpPart3, mIpPart4;
	private LayoutInflater mLayoutInflater;

	public IpAlertDialog(Context context, LayoutInflater layoutInflater) {
		super(context);
		// TODO Auto-generated constructor stub
		mLayoutInflater = layoutInflater;
		init();
	}

	public void setRange(int nWhich, int nMin, int nMax) {
		switch (nWhich) {
		case IP_PART1:
			if (mIpPart1 != null) {
				mIpPart1.setMinValue(nMin);
				mIpPart1.setMaxValue(nMax);
			}
			break;
		case IP_PART2:
			if (mIpPart2 != null) {
				mIpPart2.setMinValue(nMin);
				mIpPart2.setMaxValue(nMax);
			}
			break;
		case IP_PART3:
			if (mIpPart3 != null) {
				mIpPart3.setMinValue(nMin);
				mIpPart3.setMaxValue(nMax);
			}
			break;
		case IP_PART4:
			if (mIpPart4 != null) {
				mIpPart4.setMinValue(nMin);
				mIpPart4.setMaxValue(nMax);
			}
			break;
		default:
			break;
		}
	}

	public void setIp(int n1, int n2, int n3, int n4) {
		if (mIpPart1 != null) {
			mIpPart1.setValue(n1);
		}
		if (mIpPart2 != null) {
			mIpPart2.setValue(n2);
		}
		if (mIpPart3 != null) {
			mIpPart3.setValue(n3);
		}
		if (mIpPart4 != null) {
			mIpPart4.setValue(n4);
		}
	}

	public void setIp(String strIp) {
		String[] strIpsStrings = strIp.split("\\.");
		if (strIpsStrings.length != 4) {
			return;
		}
		mIpPart1.setValue(Integer.parseInt(strIpsStrings[0]));
		mIpPart2.setValue(Integer.parseInt(strIpsStrings[1]));
		mIpPart3.setValue(Integer.parseInt(strIpsStrings[2]));
		mIpPart4.setValue(Integer.parseInt(strIpsStrings[3]));
	}
	
	public void Invalidate() {
		mIpPart1.clearFocus();
		mIpPart2.clearFocus();
		mIpPart3.clearFocus();
		mIpPart4.clearFocus();
	}

	public String getIp() {
		if (null == mIpPart1 || null == mIpPart2 || null == mIpPart3
				|| null == mIpPart4) {
			return "";
		}
		return mIpPart1.getValue() + "." + mIpPart2.getValue() + "."
				+ mIpPart3.getValue() + "." + mIpPart4.getValue();
	}

	private void init() {
		//setCancelable(false);
		View view = mLayoutInflater.inflate(R.layout.ipaddrlayout, null);
		mIpPart1 = (NumberPicker) view.findViewById(R.id.numberPickerEx1);
		mIpPart1.setMinValue(1);
		mIpPart1.setMaxValue(254);
		mIpPart1.setValue(192);
		mIpPart2 = (NumberPicker) view.findViewById(R.id.numberPickerEx2);
		mIpPart2.setMinValue(0);
		mIpPart2.setMaxValue(254);
		mIpPart2.setValue(168);
		mIpPart3 = (NumberPicker) view.findViewById(R.id.numberPickerEx3);
		mIpPart3.setMinValue(0);
		mIpPart3.setMaxValue(254);
		mIpPart3.setValue(27);
		mIpPart4 = (NumberPicker) view.findViewById(R.id.numberPickerEx4);
		mIpPart4.setMinValue(0);
		mIpPart4.setMaxValue(254);
		mIpPart4.setValue(22);
		setTitle("IP��ַ");
		setView(view);
	}
}
